package com.deli.facetest;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements CameraSurfaceView.OnFaceCollectListener, CameraInterface.CamOpenOverCallback{

    private static final String TAG = "CameraActivity";

    private float recLen = 0;
    long te;
    float previewRate = -1f;

    private TextView textView;
    ImageView img;
    private CameraSurfaceView surfaceView = null;
    boolean flag=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread openThread = new Thread(){
            @Override
            public void run() {
                CameraInterface.getInstance().doOpenCamera(MainActivity.this);
            }
        };
        openThread.start();
        setContentView(R.layout.activity_main);
        initView();
        initViewParams();
        surfaceView.setOnFaceCollectListener(this);

        OnFaceCollectStart(false);


      //  TimeHandler.postDelayed(TimeRunnable, 1500);
    }


    /**
     * 回调方法
     * activity中的视图和 interface中的代码实现绑定
     */
    @Override
    public void cameraHasOpened() {
        SurfaceHolder holder = surfaceView.getSurfaceHolder();
        CameraInterface.getInstance().doStartPreview(holder, previewRate,surfaceView );
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        CameraInterface.getInstance().doStopCamera();
        //TimeHandler.removeCallbacks(TimeRunnable);
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        CameraInterface.getInstance().doStopCamera();
       // TimeHandler.removeCallbacks(TimeRunnable);
    }

   /* Handler TimeHandler = new Handler();
   *//* Runnable TimeRunnable = new Runnable() {
        @Override
        public void run() {
          *//**//*  recLen+=1500;
            textView.setText("检测时间：" + recLen/1000+"秒");*//**//*
            TimeHandler.postDelayed(this, 1500);
        }
    };*/


    private void initView(){
        surfaceView = (CameraSurfaceView)findViewById(R.id.camera_surfaceview);
        textView = (TextView) findViewById(R.id.time);
        img= (ImageView) findViewById(R.id.img);
    }
    private void initViewParams(){
        ViewGroup.LayoutParams params = surfaceView.getLayoutParams();
        Point p = DisplayUtil.getScreenMetrics(this);
        params.width = p.x;
        params.height = p.y;
        previewRate = DisplayUtil.getScreenRate(this); //默认全屏的比例预览
        surfaceView.setLayoutParams(params);
    }
    /**
     * 自建
     * @param bitmaps 返回的五张脸的数组
     */
    @Override
    public void OnFaceCollected(Bitmap[] bitmaps) {
        //自己进行处理 验证或者其他一些操作
        Log.d("CameraActivity","收集了1张脸");
        if(flag)
        {
            flag=false;
            img.setImageBitmap(bitmaps[0]);
           new Thread(new Runnable() {
               @Override
               public void run() {
                   try {
                       Thread.sleep(5000);
                       flag=true;
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }).start();
        }
    }

    /**
     * 自建
     * @param isStart 是否重新收集
     */
    @Override
    public void OnFaceCollectStart(boolean isStart) {
        if(isStart){
            Log.d("CameraActivity","开始收集");
        }else {
            Log.d("CameraActivity","收集结束");
        }
    }

}
